{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 167.   55.]\n",
      " [ 162.   57.]]\n",
      "0.0 [[ 0.]\n",
      " [ 1.]]\n"
     ]
    }
   ],
   "source": [
    "# 1 思想 分类器 \n",
    "# 2 如何？ 寻求一个最优的超平面 分类\n",
    "# 3 核：line\n",
    "# 4 数据：样本 \n",
    "# 5 训练  SVM_create  train predict\n",
    "# svm本质 寻求一个最优的超平面 分类\n",
    "# svm 核: line\n",
    "# 身高体重 训练 预测 \n",
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "#1 准备data\n",
    "rand1 = np.array([[155,48],[159,50],[164,53],[168,56],[172,60]])\n",
    "rand2 = np.array([[152,53],[156,55],[160,56],[172,64],[176,65]])\n",
    "\n",
    "# 2 label\n",
    "label = np.array([[0],[0],[0],[0],[0],[1],[1],[1],[1],[1]])\n",
    "\n",
    "# 3 data\n",
    "data = np.vstack((rand1,rand2))\n",
    "data = np.array(data,dtype='float32')\n",
    "\n",
    "# svm 所有的数据都要有label\n",
    "# [155,48] -- 0 女生 [152,53] ---1  男生\n",
    "# 监督学习 0 负样本 1 正样本\n",
    "\n",
    "# 4 训练\n",
    "svm = cv2.ml.SVM_create() # ml  机器学习模块 SVM_create() 创建\n",
    "# 属性设置\n",
    "svm.setType(cv2.ml.SVM_C_SVC) # svm type\n",
    "svm.setKernel(cv2.ml.SVM_LINEAR) # line\n",
    "svm.setC(0.01)\n",
    "# 训练\n",
    "result = svm.train(data,cv2.ml.ROW_SAMPLE,label)\n",
    "# 预测\n",
    "pt_data = np.vstack([[167,55],[162,57]]) #0 女生 1男生\n",
    "pt_data = np.array(pt_data,dtype='float32')\n",
    "print(pt_data)\n",
    "(par1,par2) = svm.predict(pt_data)\n",
    "print(par2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
